<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Offerform extends MY_Controller {

	function __construct() {
		parent::__construct();
	}
	
	public function index($offer_id)	{
		if ($this->isSessionActive()) {


			
			$this->load->model('offer_model');
			
			$data['user'] = $this->fb_id;
			$data['title'] = 'Add New Offer';
			$data['content_view'] = 'offerform_view';
			$data['stylesheets'] = array('starrating', 'offerform');
			$data['javascripts'] = array('offerform','starrating_offerform');
			$data['sub_category'] = $this->offer_model->get_sub_category(); // list all sub_category available
			
			
			// preview image
			if ($offer_id > 0) {
				$data['offer_info'] = $this->offer_model->get_offer($offer_id);
				if ($data['user'] != $data['offer_info']['fb_id']) 
					redirect('/home','refresh');
			} else {
				$default_offer_info['offer_id'] = 0;
				$default_offer_info['item_title'] = '';
				$default_offer_info['item_price'] = 0.0;
				$default_offer_info['item_condition'] = 0;
				$default_offer_info['age_year'] = 0;
				$default_offer_info['age_month'] = 0;
				$default_offer_info['item_desc'] = '';
				$default_offer_info['item_pic'] = 'miscellaneous_default.jpg';
				$default_offer_info['category_id'] = 0;
				$default_offer_info['sub_category_id'] = 0;
				$data['offer_info'] = $default_offer_info;
			}
			if ($this->input->post('upload_pic')) {
				
				$config = array(
					'allowed_types' => 'jpg|gif|png|jpeg',
					'upload_path' => realpath(APPPATH . '../resources/offer_images/'),
					'max_size' => '3072',
					'remove_space' => true
				);
				$this->load->library('upload', $config);
				
				// Upload an image
				if (!$this->upload->do_upload()){
					$data['upload_file_error'] = $this->upload->display_errors('<span style="color: red; font-weight: bold;">', '</span>');
				}
				else {
					// remove previous upload if it's not a default image.
					/* not working right now
					$default_image_array = array ("miscellaneous_default.jpg", "appliances_default.jpg",
											"apparel_default.jpg", "electronics_default.jpg", 
											"equipment_and_tools_default.jpg", "furniture_default.jpg", 
											"games_default.jpg", "handheld_devices_default.jpg",
											"paper_default.jpg", "stationery_default.jpg");
					echo $data['offer_info']['item_pic'];
					echo in_array($data['offer_info']['item_pic'], $default_image_array);
					if (in_array($data['offer_info']['item_pic'], $default_image_array) == false) {				
						echo base_url() .  "resources/offer_images/" . $data['offer_info']['item_pic'];
						if (unlink(base_url() .  "resources/offer_images/" . $data['offer_info']['item_pic']))
							echo "file is deleted";
						unlink(base_url() .  "resources/offer_images_thumbnail/" . $data['offer_info']['item_pic']);
					}
					*/
					// update image
					$image_data = $this->upload->data();
					$data['offer_info']['item_pic'] = $image_data['file_name'];
					$config_thumb = array(
									'source_image' => $image_data['full_path'] , //get original image
									'new_image' => realpath(APPPATH . '../resources/offer_images_thumbnail/') . '/', //save as new image //need to create thumbs first
									'maintain_ratio' => true,
									'width' => 200,
									'height' => 200
								);
 					$this->load->library('image_lib', $config_thumb); //load library
					// Create a 200x200 thumbnail
					if ( ! $this->image_lib->resize()) {
						echo $this->image_lib->display_errors('<span style="color: red; font-weight: bold;">', '</span>');
					}
				}
			} else {
				if ($this->input->post('offer_submit_x')) {
					$this->load->library('form_validation');
					$this->form_validation->set_error_delimiters('<span style="color: red; font-weight: bold;">', '</span>');
					$this->form_validation->set_rules('item_title', 'Item Title', 'trim|required');
					$this->form_validation->set_rules('item_pic_name', 'Item Picture', 'required');
					$this->form_validation->set_rules('item_condition', 'Item Condition', 'required');
					$this->form_validation->set_rules('item_desc', 'Item Description', 'trim|required');
					$this->form_validation->set_rules('age_year', 'Age Year', 'required');
					$this->form_validation->set_rules('age_month', 'Age Month', 'required');
					$this->form_validation->set_rules('category_id', 'Category', 'required');
					$this->form_validation->set_rules('sub_category_id', 'Subcategory', 'required');
					$this->form_validation->set_rules('price_choice', 'Price Choice', 'required');
					$this->form_validation->set_rules('item_price', 'Item Price', 'is_price');
					// to reserve the image if validations fail
					if($this->input->post('item_pic_name')){
						$data['offer_info']['item_pic'] = $this->input->post('item_pic_name');
					}
					
					if ($this->form_validation->run() == TRUE) {
						$data['offer_info']['item_pic'] = $this->input->post('item_pic_name');
						$data['offer_info']['item_title'] = $this->input->post('item_title');
						$data['offer_info']['item_price'] = $this->input->post('item_price');
						$data['offer_info']['item_condition'] = $this->input->post('item_condition');
						$data['offer_info']['item_desc'] = $this->input->post('item_desc');
						$data['offer_info']['age_year'] = $this->input->post('age_year');
						$data['offer_info']['age_month'] = $this->input->post('age_month');
						$data['offer_info']['category_id'] = $this->input->post('category_id');
						$data['offer_info']['sub_category_id'] = $this->input->post('sub_category_id');
						if ($data['offer_info']['offer_id'] == 0) {
							$new_offer_id = $this->offer_model->submit_offer($data['offer_info']['item_title'],$data['offer_info']['item_price'],
																			$data['offer_info']['item_pic'],$data['offer_info']['item_condition'],
																			$data['offer_info']['item_desc'],$data['offer_info']['age_year'],
																			$data['offer_info']['age_month'],$data['offer_info']['category_id'],
																			$data['offer_info']['sub_category_id'],$this->fb_id);
							$this->add_status_message('Your New offer has been successfully created!');
							redirect('/home/','refresh');
						}
						else {
							$this->offer_model->update_offer($data['offer_info']['offer_id'],$data['offer_info']['item_title'],
																$data['offer_info']['item_price'],$data['offer_info']['item_pic'],
																$data['offer_info']['item_condition'],$data['offer_info']['item_desc'],
																$data['offer_info']['age_year'],$data['offer_info']['age_month'],
																$data['offer_info']['category_id'],$data['offer_info']['sub_category_id'],
																$this->fb_id);
							$this->add_status_message('Your Offer has been edited!');
							redirect('/itemdescription/' . $data['offer_info']['offer_id'],'refresh');
						}
						
					}	
				}
			}
				
			$this->load_template_view($data, TRUE);
		}
	}
}